package com.oneinlet

import java.text.DecimalFormat

/**
 * Created by WangZiHe on 2018/12/24
 * QQ/WeChat:648830605
 * QQ-Group:368512253
 * Blog:www.520code.net
 * Github:https://github.com/yancheng199287
 */
object YCFormat {

    /**
     * 将字节单位容量转换成合适理解的单位容量
     * @param size  接收一个容量值  字节B
     * @return  返回到达某个级别单位的值
     * */
    fun formatSpaceSize(size: Long): String {
        if (size <= 0) return "0"
        val units = arrayOf("B", "kB", "MB", "GB", "TB")
        val digitGroups = (Math.log10(size.toDouble()) / Math.log10(1024.0)).toInt()
        return DecimalFormat("#,##0.#").format(size / Math.pow(1024.0, digitGroups.toDouble())) + " " + units[digitGroups]
    }

}